package com.abc.robot01.room

import android.content.Context
import android.util.Log
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.abc.robot01.activity.BaseVoiceActivity.Companion.TAG
import com.abc.robot01.room.bean.ChatAnswerBean
import com.abc.robot01.room.bean.ChatQuestionBean
import com.abc.robot01.room.bean.CmdBean
import com.abc.robot01.room.dao.ChatAnswerDao
import com.abc.robot01.room.dao.ChatQuestionDao
import com.abc.robot01.room.dao.CmdDao
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ChatAnswerTest {
    private lateinit var chatAnswerDao: ChatAnswerDao
    private lateinit var chatQuestionDao: ChatQuestionDao
    private lateinit var cmdDao: CmdDao

    private lateinit var db: AppDatabase

    @Before
    fun setUp() {
        val context = ApplicationProvider.getApplicationContext<Context>()
        db = Room.inMemoryDatabaseBuilder(
            context, AppDatabase::class.java).build()
        chatAnswerDao = db.chatAnswerDao()
        chatQuestionDao=db.chatQuestionDao()
        cmdDao=db.cmdDao()
    }

    @Test
    @Throws(Exception::class)
    fun addAnswer(){
        addData()
        val chatList = chatAnswerDao.qryAllAnswer()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
    }

    private fun addData() {
        val q1= ChatAnswerBean().apply {
            cmd_Id = "12"
            type = 0
        }
        val q2 = ChatAnswerBean().apply {
            cmd_Id = "15"
            type = 1
        }
        chatAnswerDao.addAnswer(q1)
        chatAnswerDao.addAnswer(q2)
    }

    private fun addData2() {
        val id = cmdDao.addCmd(CmdBean().apply { name ="第一条定制" })
        val q1= ChatAnswerBean().apply {
            cmd_Id = id.toString()
            type = 0
            ttsText="机器人拾音模式分为单次拾音和持续拾音两种模式"
        }
        chatAnswerDao.addAnswer(q1)

    val q = ChatQuestionBean().apply {
        cmd_id = id.toString()
        type = 0
        question = "你好"
    }
        chatQuestionDao.addQuestion(q)
    }

    fun  getAnswer(question:String):String?{
        Log.e(TAG, "查询问题：$question");
        val questions = DatabaseContext.getChatQuestionDao().queryCmd(question) ?: return null
        Log.e(TAG, "Ok，存在：$question");
        return DatabaseContext.getChatAnswerDao().queryAnswerByCmdId(questions.cmd_id)[0].ttsText
    }

    @Test
    @Throws(Exception::class)
    fun qryAllAnswer2(){
        Assert.assertNotNull(
        getAnswer("房门"))


    }




    @Test
    @Throws(Exception::class)
    fun qryAllAnswer(){
        addData()
        val chatList = chatAnswerDao.qryAllAnswer()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
    }

    @Test
    @Throws(Exception::class)
    fun queryAnswerByCmdId(){
        addData()
        val chatList = chatAnswerDao.qryAllAnswer()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
        val resultList = chatAnswerDao.queryAnswerByCmdId(chatList!![0].cmd_Id)
        Assert.assertNotNull(resultList)
        Assert.assertEquals(resultList.size, 1)
        Assert.assertEquals(resultList!![0].cmd_Id, "12")
    }

    @Test
    @Throws(Exception::class)
    fun deleteAnswer(){
        addData()
        val chatList = chatAnswerDao.qryAllAnswer()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
        val code = chatAnswerDao.deleteAnswer(chatList!![0].cmd_Id)
        Assert.assertNotNull(code == 1)
        val resultList = chatAnswerDao.qryAllAnswer()
        Assert.assertNotNull(resultList)
        Assert.assertEquals(resultList.size, 1)
    }

    @Test
    @Throws(Exception::class)
    fun queryAnswerByQuestion(){
        addData2()
        val cmd = chatQuestionDao.queryCmd("你好")!!.cmd_id
        Assert.assertEquals(chatAnswerDao.queryAnswerByCmdId(cmd)[0].ttsText, "机器人拾音模式分为单次拾音和持续拾音两种模式")
    }

}